using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace autoMessage
{
    //本类中的代码出自 https://blog.csdn.net/qq_53217825/article/details/125987802
    public class EmailHelper
    {
        public static EmailHelper MailSystem163 = new EmailHelper(
           mail =>
           {
               mail.Host = "smtp.163.com";//使用163的SMTP服务器发送邮件
                                          //mail.UseDefaultCredentials = true;//在winform平台使用默认值
               mail.EnableSsl = true;
               mail.UseDefaultCredentials = false;//在.framework或mvc下使用这个
               mail.Port = 25;//端口号
               mail.DeliveryMethod = SmtpDeliveryMethod.Network;
               mail.Credentials = new System.Net.NetworkCredential("17663981905@163.com", "");
           },
           "17663981905@163.com",
           "17663981905@163.com"
       );

        /// <summary>
        /// 系统连续发送失败的次数，为了不影响系统，连续三次失败就禁止发送
        /// </summary>
        private static long SoftMailSendFailedCount { get; set; } = 0L;


        /// <summary>
        /// 系统的邮件发送客户端
        /// </summary>
        private SmtpClient smtpClient { get; set; }

        /// <summary>
        /// 发送邮件的地址
        /// </summary>
        private string MailFromAddress { get; set; } = "";


        /// <summary>
        /// 邮件发送的地址
        /// </summary>
        public string MailSendAddress { get; set; } = "";


        /// <summary>
        /// 实例化一个邮箱发送类，需要指定初始化信息
        /// </summary>
        /// <param name="mailIni">初始化的方法</param>
        /// <param name="addr_From">发送地址，应该和账户匹配</param>
        /// <param name="addr_to">邮件接收地址</param>
        /// <remarks>
        /// </remarks>
        public EmailHelper(Action<SmtpClient> mailIni, string addr_From = "", string addr_to = "")
        {
            smtpClient = new SmtpClient();
            mailIni(smtpClient);
            MailFromAddress = addr_From;
            MailSendAddress = addr_to;
        }

        private string GetExceptionMail(Exception ex)
        {
            return ex.Message;
        }

        /// <summary>
        /// 发送邮件的方法，需要提供完整的参数信息
        /// </summary>
        /// <param name="addr_from">发送地址</param>
        /// <param name="name">发送别名</param>
        /// <param name="addr_to">接收地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="attachment">附件地址</param>
        /// <param name="isHtml">邮件内容是否是HTML语言</param>
        /// <param name="priority">优先级</param>
        /// <returns>发生是否成功，内容不正确会被视为垃圾邮件</returns>
        public bool SendMail(string addr_from, string name, List<string> addr_to, string subject, string body, string[]? attachment, bool isHtml = false, MailPriority priority = MailPriority.Normal)
        {
            if (SoftMailSendFailedCount > 10)
            {
                SoftMailSendFailedCount++;
                return true;
            }
            MailMessage mailMessage = new MailMessage();
            try
            {
                mailMessage.From = new MailAddress(addr_from, name, Encoding.UTF8);
                foreach (string addresses in addr_to)
                {
                    mailMessage.To.Add(addresses);
                }
                mailMessage.Subject = subject;
                mailMessage.Body = body;
                MailMessage mailMessage2 = mailMessage;
                mailMessage2.Body = mailMessage2.Body + Environment.NewLine + Environment.NewLine + Environment.NewLine;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.Priority = priority;
                mailMessage.IsBodyHtml = isHtml;

                if (attachment is not null)
                    for (int i = 0; i < attachment.Length; i++)
                    {
                        Attachment address = new Attachment(attachment[i]);
                        mailMessage.Attachments.Add(address);
                    }
                smtpClient.Send(mailMessage);
                SoftMailSendFailedCount = 0L;
                return true;
            }
            catch (Exception)
            {
                SoftMailSendFailedCount++;
                return false;
            }
        }
    }
}
